/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.google.code.japidiff;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Collection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.objectweb.asm.ClassReader;

public class Main {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
        Collection<JavaClass> classes = read(args[0]);
        Printer printer = new Printer();
        printer.publicApis = true;
        if (args.length > 0) {
            printer.classNames = args[1];
        }
        StringBuilder builder = new StringBuilder();
        printer.printClasses(classes, builder);
        System.out.println(builder.toString());
    }

	
	public static void diff(String jar1, String jar2) throws IOException {
		read(jar1);
	}

	public static Collection<JavaClass> read(String jar) throws IOException {
		JavaClassBuilder v = new JavaClassBuilder();
		ZipFile f = new ZipFile(jar);
		Enumeration<? extends ZipEntry> en = f.entries();
		while (en.hasMoreElements()) {
			ZipEntry e = en.nextElement();
			String name = e.getName();
			if (name.endsWith(".class")) {
				ClassReader cr = new ClassReader(f.getInputStream(e));
				cr.accept(v, 0);
			}
		}
        return v.getClasses();
    }

}
